from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QMainWindow, QMessageBox

from Tools.LoginThread import LoginThread
from Ui.LoginUi import Ui_MainWindow


class Login(QMainWindow):
    def __init__(self):
        super(Login, self).__init__()

        self.loginThread = LoginThread()
        self.loginThread.loginSignal.connect(self.loginThreadBack)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.setWindowFlags(Qt.FramelessWindowHint)

    def userLogin(self):
        user = self.ui.username.text()
        pwd = self.ui.password.text()
        self.loginThread.setUser(user, pwd)
        if len(user) == 0 or len(pwd) == 0:
            QMessageBox.warning(self, "警告", "用户名或密码错误")
            self.ui.username.setText("")
            self.ui.password.setText("")
        else:
            self.ui.login_pushButton.setText("正在登录中...")
            self.loginThread.start()
            # print(user+"  "+pwd)

    def loginThreadBack(self, backcode):
        if backcode == 1:
            self.ui.login_pushButton.setText("登录成功")
        else:
            self.ui.login_pushButton.setText("登录失败")
